/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action.room;

import dao.RoomDAO;
import dao.RoomImageDAO;
import dto.RoomDTO;
import dto.RoomImageDTO;
import java.io.Console;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Lada
 */
public class CreateRoomAction extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String mode = request.getParameter("mode") == null ? "" : request.getParameter("mode");
        
    
//            if (mode.equals("insert")) {
                String typeS = request.getParameter("typeid") == null ? "" : request.getParameter("typeid");
                    String roomNoS = request.getParameter("roomno") == null ? "" : request.getParameter("roomno");
                    String bedS = request.getParameter("beds") == null ? "" : request.getParameter("beds");
                    String floorS = request.getParameter("floor") == null ? "" : request.getParameter("floor");
                    if (typeS != "" && bedS != "" && floorS != "") {
                        int typeid = Integer.parseInt(typeS);
                        int beds = Integer.parseInt(bedS);
                        int floor = Integer.parseInt(floorS);
                        RoomDAO rDao = new RoomDAO();
                        RoomDTO dto = new RoomDTO(1, roomNoS, floor, beds, typeid);
                        int roomID = rDao.create(dto);
                        RoomImageDAO rimDao = new RoomImageDAO();
                        HttpSession session = request.getSession();
                        ArrayList<String> fileList = (ArrayList<String>)session.getAttribute("fileList");
                        for (String string : fileList) {
                            RoomImageDTO rmDTO = new RoomImageDTO(roomID, string);
                            RoomImageDTO r = rimDao.create(rmDTO);
                        }
                    }
//            }
        

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
//    @Override
//    public String getServletInfo() {
//        return "Short description";
}
// </editor-fold>

